package com.shiroa.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.shiroa.common.R;
import com.shiroa.common.Token;
import com.shiroa.entity.vo.LoginVo;
import com.shiroa.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginException;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) throws LoginException, JsonProcessingException {
      Token token   = loginService.sslogin(loginVo);
      return R.ok(token);
    }

//    @PostMapping("/login")
//    public R login(@RequestBody LoginVo loginVo)   {
//        try {
//            Token token = loginService.login(loginVo);
//            return R.ok(token);
//        }catch (Exception e){
//            return R.err(e.getMessage());
//        }
//    }

//    @PostMapping("/login")
//    public R login(@RequestBody LoginVo loginVo)   {
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
//        try {
//            SecurityUtils.getSubject().login(usernamePasswordToken);
//        }catch (Exception e){
//            return R.err(e.getMessage());
//        }
//        return R.ok();
//    }
    @GetMapping("/logout")
    public R logout(@RequestHeader String token){
        try {
            loginService.logout(token);
            return R.ok(token);
        }catch (Exception e){
            return R.err(e.getMessage());
        }
    }
}